/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.bean;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.cjava.system.model.SystemUser;
import org.cjava.system.service.interfac.ISystemUserService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;
import org.omnifaces.util.Faces;

/**
 *
 * @author max
 */
@ManagedBean(name = "loginUserBean")
@SessionScoped
public class LoginUserBean implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @ManagedProperty("#{systemUserService}")
    ISystemUserService systemUserService;
    /////////////////////////////////////////
    @ManagedProperty("#{navigationBean}")
    NavigationBean navigationBean;
    private SystemUser systemUser = new SystemUser();
    private String userName;
    private String password;

    
    public void login() throws Exception {
        System.out.println(userName);
        System.out.println(password);
        String hql = "from SystemUser s where s.userName='" + userName + "' "
                + "and s.password='" + password + "' "
                + "and (s.type='" + Clazz._TYPE_SYSTEM_USER._ADMIN_CONFIG + "' "
                + "or s.type='" + Clazz._TYPE_SYSTEM_USER._ADMIN_USER +"')";
        List<SystemUser> listSys = systemUserService.listHQL(hql);
        if( listSys.isEmpty() ) {
            FacesUtil.addMessage("Contraseña incorrecta", null, FacesMessage.SEVERITY_ERROR);
        } else {
            systemUser = listSys.get(0);
            if(systemUser.getType().equals(Clazz._TYPE_SYSTEM_USER._ADMIN_CONFIG)) {
                navigationBean.setNavigationMenu("./pages/menu/configuration.xhtml");
                Faces.setSessionAttribute(FacesUtil.ATTRIBUTE_USER, systemUser);
                
            } else if(systemUser.getType().equals(Clazz._TYPE_SYSTEM_USER._ADMIN_USER)) {
                navigationBean.setNavigationMenu("./pages/menu/admin.xhtml");
                Faces.setSessionAttribute(FacesUtil.ATTRIBUTE_USER, systemUser);
               
            } else if(systemUser.getType().equals(Clazz._TYPE_SYSTEM_USER._EMPRESA)) {
                FacesUtil.addMessage("Usuario destinado a Empresa", null, FacesMessage.SEVERITY_WARN);
            } else if(systemUser.getType().equals(Clazz._TYPE_SYSTEM_USER._SOCIO)) {
                FacesUtil.addMessage("Usuario destinado a Socio", null, FacesMessage.SEVERITY_WARN);
            }
        }
          Faces.redirect("index.jsf");  
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

    public void setSystemUserService(ISystemUserService systemUserService) {
        this.systemUserService = systemUserService;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    
}
